package com.ysy.tz.core.selfcomp;

import com.ysy.tz.core.engine.ApplicationContext;
import com.ysy.tz.core.engine.CustomerFXMLLoader;
import com.ysy.tz.core.utils.FileResourceLoader;
import java.io.IOException;
import java.util.ResourceBundle;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;

public class CommonDialog {
    private static Dialog dialog = new Dialog();
    private static Dialog dialog2;
    private static Dialog dialog3;

    public CommonDialog() {
    }

    public static Dialog loadDialogPane(DialogPane dialogPane, String title) {
        Dialog currentDialog = getCacheDialog();
        currentDialog.setDialogPane(dialogPane);
        currentDialog.setTitle(title);
        return currentDialog;
    }

    private static Dialog getCacheDialog() {
        Dialog currentDialog = null;
        if (!dialog.isShowing()) {
            currentDialog = dialog;
        } else {
            if (dialog2 == null) {
                dialog2 = new Dialog();
            }

            if (!dialog2.isShowing()) {
                currentDialog = dialog2;
            } else {
                if (dialog3 == null) {
                    dialog3 = new Dialog();
                }

                currentDialog = dialog3;
            }
        }

        return currentDialog;
    }

    public static Dialog loadFXMLDialog(String titile, String DialogName, String bundleName, Object... params) {
        Dialog currentDialog = getCacheDialog();
        CustomerFXMLLoader customerFXMLLoader = new CustomerFXMLLoader();

        try {
            if (StringUtils.isNotBlank(bundleName)) {
                customerFXMLLoader.setResources(ResourceBundle.getBundle("views/i18n/" + bundleName));
            }

            DialogPane dialogPane = null;
            if (params != null) {
                dialogPane = (DialogPane)customerFXMLLoader.load(FileResourceLoader.loadInputStream("/views/" + DialogName + "Dialog.fxml"), params);
            } else {
                dialogPane = (DialogPane)customerFXMLLoader.load(FileResourceLoader.loadInputStream("/views/" + DialogName + "Dialog.fxml"));
            }

            currentDialog.setDialogPane(dialogPane);
            currentDialog.setTitle(titile);
            Stage stage = (Stage)dialogPane.getScene().getWindow();
            Object controller = customerFXMLLoader.getController();
            if (controller != null) {
                ApplicationContext.eventBus.register(controller);
            }

            stage.setOnHidden((windowEvent) -> {
                if (controller != null) {
                    ApplicationContext.eventBus.unregister(controller);
                }

            });
        } catch (IOException var9) {
            var9.printStackTrace();
        }

        return currentDialog;
    }

    static {
        dialog.initOwner(ApplicationContext.mainStage);
    }
}
